//
//  RJBarTop.m
//  AirQuality
//
//  Created by Po on 15/4/2.
//  Copyright (c) 2015年 Po. All rights reserved.
//

#import "RJBarTop.h"
@interface RJBarTop ()
@property (retain, nonatomic) UILabel * label;
@property (retain, nonatomic) UIColor * color;
@property (retain, nonatomic) NSString * num;
@property (assign, nonatomic) CGRect trueRect;

@property (assign, nonatomic) BOOL isAnim;

@end

@implementation RJBarTop

- (instancetype)initWithFrame:(CGRect)frame color:(UIColor *)color num:(NSString *)num
{
    _trueRect = frame;
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor clearColor]];
        _color = color;
        _num = num;
        _isAnim = YES;
        [self addSubview:[self getTittleLabel:self.bounds title:num]];
//        [self startAnim];
    }
    return self;
}

//- (void)drawRect:(CGRect)rect
//{
//    CGFloat width = self.frame.size.width;
//    CGFloat height = self.frame.size.height;
//    CGFloat radius = width / 2;
//    //外部边框
//    CGContextRef ref = UIGraphicsGetCurrentContext();
//    CGContextSetFillColorWithColor(ref, _color.CGColor);
//    CGContextMoveToPoint(ref, radius, radius);
//    CGContextAddArc(ref, radius, radius, radius,  60 * M_PI / 180, 120 * M_PI / 180, 1);
//    CGContextMoveToPoint(ref, radius, 2 * radius + 10);
//    CGContextAddArc(ref, radius, radius, radius,  60 * M_PI / 180, 120 * M_PI / 180, 1);
//    CGContextClosePath(ref);
//    CGContextFillPath(ref);
//    CGContextStrokePath(ref);
//    
//    //内部圆形与数值
//    CGContextMoveToPoint(ref, radius, radius);
//    CGContextSetFillColorWithColor(ref, [UIColor whiteColor].CGColor);
//    CGContextFillEllipseInRect(ref, CGRectMake(5, 5, width - 10, width - 10));
//    CGContextStrokePath(ref);
//    
//    //写字
//    CGContextSetLineWidth(ref, 2.0);
//    CGContextSetFillColorWithColor(ref, [UIColor redColor].CGColor);
//    
//    CGRect mrect = [_num boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesFontLeading attributes:@{@"NSFontAttributeName" : [UIFont systemFontOfSize:10]} context:nil];
//    
//    //计算大小并居中
//    CGFloat leftX = (width - mrect.size.width) / 2;
//    CGFloat leftY = (height - mrect.size.height) / 2;
//    
//    [_num drawWithRect:CGRectMake(leftX, leftY, width - 2 *leftX, height - leftY * 2) options:NSStringDrawingUsesFontLeading attributes:@{@"NSFontAttributeName" : [UIFont systemFontOfSize:10]} context:nil];
//    
//}

#pragma mark - anim
/**是无限循环*/
- (void)startAnim
{
    [UIView animateWithDuration:0.5f animations:^{
        CGRect rect = _trueRect;
        rect.origin.y += 10;
        [self setFrame:rect];
    } completion:^(BOOL finished) {
        if (_isAnim) {
            [UIView animateWithDuration:0.5f animations:^{
                [self setFrame:_trueRect];
            } completion:^(BOOL finished) {
                [self startAnim];
            }];
        }
    }];
}

#pragma mark - function
- (void)showHighlightShow
{
    if (!_isAnim) {
        return;
    }
    [self.layer removeAllAnimations];
    _isAnim = NO;
    CGRect rect = _trueRect;
    rect.origin.x -= 10;
    rect.origin.y -= 10;
    rect.size.width += 20;
    rect.size.height += 20;
    [self setFrame:rect];
}

- (void)reSetShow
{
    if (_isAnim) {
        return;
    }
    _isAnim = YES;
    [self setFrame:_trueRect];
    [self startAnim];
}

- (void)removeAllAnim
{
    _isAnim = NO;
    [self.layer removeAllAnimations];
}

//数值
- (UILabel *)getTittleLabel:(CGRect)rect title:(NSString *)title
{
    if (!_label) {
        _label = [[UILabel alloc] initWithFrame:rect];
        [_label setText:title];
        [_label  setFont:[UIFont systemFontOfSize:self.frame.size.width / 3]];
        [_label setTextColor:[UIColor whiteColor]];
        [_label setTextAlignment:NSTextAlignmentCenter];
    }
    return _label;
}
@end
